import requests
import os
from lxml import etree
import re

headers = {
    'user-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'
}


url = "https://www.yesbobbleheads.com/head-to-toe-custom"


#####包装产品信息下载函数####
def product_get(url,headers):

    ###获取目标xpath###
    page_text = requests.get(url=url, headers=headers).text
    tree = etree.HTML(page_text)
    type = tree.xpath('//div[@class="page-body"]/h1/text()')
    pic = tree.xpath('//div[@class="product-grid"]//a/img/@src')
    title = tree.xpath('//div[@class="product-grid"]//h2/a/text()')
    price = tree.xpath('//span[@class="price actual-price"]/text()')

    ###把所有信息合并到列表中######
    product = list(zip(title,price,pic))

    ####进入逐个逐个的安排#####
    for i in product:

        ###新建分类文件夹###
        fold = "./bobble/{}".format(type[0])
        if not os.path.exists(fold):
            os.mkdir(fold)

        ###按规则的名称持久化###

        ###############
        ####获取图片数据#######
        img_data = requests.get(i[2],headers).content
        ######图片命名#######
        img_name = i[0].replace(':',' ').replace('"','inch')+"__"+i[1]+".jpg"

        #####图片路径和命名汇总#######
        img_save = "{}/{}".format(fold,img_name)

        ########保存下来######
        with open(img_save, 'wb') as fp:
            fp.write(img_data)
            print(img_name, '下载成功！！')





product_get(url,headers)



# for i in url:
#     product_get(i, headers)

#####需要获取不同页码urls(带有last的产品分类）时候再激活用该代码#########
# page_text = requests.get(url=url, headers=headers).text
# tree = etree.HTML(page_text)
# page_all = tree.xpath('//li[@class="last-page"]/a/@href')[0]
# numbers = page_all.split('=')[1]
# for i in range(1, int(numbers)+1):
#     next_page = "https://www.yesbobbleheads.com"+page_all.split('=')[0]+"="+str(i)
#     # print (next_page)
#     product_get(next_page,headers)